﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;



public partial class Raspored2 : System.Web.UI.Page
{
    public struct Niza
    {
        public List<string> den;
        public List<string> od;
        public List<string> doo;
        public List<string> imence;
        public List<string> predmet;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblIme.Text = "Име: " + Session["ime"].ToString()+" ЏАГААА";
            lblPrezime.Text = "Презиме: " + Session["prezime"].ToString();
            lblIndeks.Text = "Индекс: " + Session["indeks"].ToString();
            Like_Totaly_Awesome_Function();
        }
    }


    protected void Like_Totaly_Awesome_Function()
    {
        Niza raspored = rasp();
        List<string> den = raspored.den;
        List<string> od = raspored.od;
        List<string> doo = raspored.doo;
        List<string> ime = raspored.imence;
        List<string> predmet = raspored.predmet;
        string [] denovi = { "Понеделник", "Вторник", "Среда", "Четврток", "Петок" };
        int br12 = 2, br13 = 3;
        bool br1 = false;
        string name1 = string.Empty, predmet1 = string.Empty;
        pnlPanela.Controls.Add(new LiteralControl("<div id='wrapper'><div>"));
        pnlPanela.Controls.Add(new LiteralControl("<table id='tabela' >"));
        pnlPanela.Controls.Add(new LiteralControl("<colgroup>"));
        pnlPanela.Controls.Add(new LiteralControl("<col class='even'/>"));
        pnlPanela.Controls.Add(new LiteralControl("<col class='odd'/>"));
        pnlPanela.Controls.Add(new LiteralControl("<col class='even'/>"));
        pnlPanela.Controls.Add(new LiteralControl("<col class='odd'/>"));
        pnlPanela.Controls.Add(new LiteralControl("<col class='even'/>"));
        pnlPanela.Controls.Add(new LiteralControl("<col class='odd' />"));
        pnlPanela.Controls.Add(new LiteralControl("</colgroup>"));
        pnlPanela.Controls.Add(new LiteralControl("<thead><th align='center' scope='col' id='par'>Час</th>"));
        
        pnlPanela.Controls.Add(new LiteralControl("</tr></thead><tbody>"));

        pnlPanela.Controls.Add(new LiteralControl("<tr><td align='center'>08:00-08:45</td></tr>"));
        pnlPanela.Controls.Add(new LiteralControl("<tr><td align='center'>09:00-09:45</td></tr>"));
        pnlPanela.Controls.Add(new LiteralControl("<tr><td align='center'>10:00-10:45</td></tr>"));
        pnlPanela.Controls.Add(new LiteralControl("<tr><td align='center'>11:00-11:45</td></tr>"));
        pnlPanela.Controls.Add(new LiteralControl("<tr><td align='center'>12:00-12:45</td></tr>"));
        pnlPanela.Controls.Add(new LiteralControl("<tr><td align='center'>13:00-13:45</td></tr>"));
        pnlPanela.Controls.Add(new LiteralControl("<tr><td align='center'>14:00-14:45</td></tr>"));
        pnlPanela.Controls.Add(new LiteralControl("<tr><td align='center'>15:00-15:45</td></tr>"));
        pnlPanela.Controls.Add(new LiteralControl("<tr><td align='center'>16:00-16:45</td></tr>"));
        pnlPanela.Controls.Add(new LiteralControl("<tr><td align='center'>17:00-17:45</td></tr>"));
        pnlPanela.Controls.Add(new LiteralControl("<tr><td align='center'>18:00-18:45</td></tr>"));
        pnlPanela.Controls.Add(new LiteralControl("<tr><td align='center'>19:00-19:45</td></tr></tbody></table>"));
        pnlPanela.Controls.Add(new LiteralControl("</div>"));
        for (int j = 0; j < 5; j++)
        {   
            // OVOJ DEL MOZE DIREKTNO DOLE VO ID DA ODI SO TERNAREN...TREBA DA VIDAM KAKO RABOTI SAMO TERNARNIOT :P ripkeeeeeeeeeeeeeeeeeeeeeeeee
            string par;
            if(j%2 == 0) par="nep";
            else par="par";
            //<<<<<<<<<<<<<<<<<<<<<           >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
            if(j<4)
                pnlPanela.Controls.Add(new LiteralControl("<div>"));
            else
                pnlPanela.Controls.Add(new LiteralControl("<div id='fifth'>"));
            //<<<<<<<<<<<<<<<<<<<<<           >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
            pnlPanela.Controls.Add(new LiteralControl("a<table id='tabela' >"));
            pnlPanela.Controls.Add(new LiteralControl("<colgroup>"));
            pnlPanela.Controls.Add(new LiteralControl("<col class='even'/>"));
            pnlPanela.Controls.Add(new LiteralControl("<col class='odd'/>"));
            pnlPanela.Controls.Add(new LiteralControl("<col class='even'/>"));
            pnlPanela.Controls.Add(new LiteralControl("<col class='odd'/>"));
            pnlPanela.Controls.Add(new LiteralControl("<col class='even'/>"));
            pnlPanela.Controls.Add(new LiteralControl("<col class='odd' />"));
            pnlPanela.Controls.Add(new LiteralControl("</colgroup>"));
            pnlPanela.Controls.Add(new LiteralControl("<thead><th align='center' scope='col' id='"+par+"'>" + denovi[j] +"</th>"));
            pnlPanela.Controls.Add(new LiteralControl("</tr></thead><tbody>"));
            
            for (int i = 0; i < 12; i++)
            {
                pnlPanela.Controls.Add(new LiteralControl("<tr>"));
                bool z1 = false;
                int h1 = 1;
                
                for (int k = 0; k < od.Count; k++)
                {
                    int p;
                    int a;
                    int b = Convert.ToInt32(od[k]);

                    if ((b - 8) == i)
                    {
                        a = Convert.ToInt32(doo[k]); // se zema koga zavrsuva predavanjeto
                        int rez = a - b + 1; // presmetka za kolku casa trae predavanjeto
                        p = Convert.ToInt32(den[k]); // se gleda koj den e predavanjeto
                        if (p == j+1)
                        {
                            z1 = true; h1 = rez; name1 = ime[k]; predmet1 = predmet[k];
                        }
                    }
                }                
                if (h1 == 2) { br12 = 0; br1 = true; }
                if (h1 == 3) { br13 = 0; br1 = true; }


                if (z1) pnlPanela.Controls.Add(new LiteralControl("<td  align='center' rowspan=" + h1 + "><h2>" + predmet1 + "</h2><br/><font color=#808080>" + name1 + "</font></td>"));
                else if (br12 == 2 && br13 == 3) pnlPanela.Controls.Add(new LiteralControl("<td></td>"));

                pnlPanela.Controls.Add(new LiteralControl("</tr>"));
                if (br1)
                {
                    if (br12 != 2) br12++;
                    if (br13 != 3) br13++;
                    if (br12 == 2 && br13 == 3) br1 = false;
                }                
            }
            pnlPanela.Controls.Add(new LiteralControl("</tbody>"));
            pnlPanela.Controls.Add(new LiteralControl("</table>"));
            //<<<<<<<<<<<<<<<<<<<<<           >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
            pnlPanela.Controls.Add(new LiteralControl("</div>"));            
            //<<<<<<<<<<<<<<<<<<<<<           >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        }
        pnlPanela.Controls.Add(new LiteralControl("</div>"));
    }



    protected Niza rasp()
    {
        string ime = Session["korisnicko"].ToString();
        SqlConnection konekcija = new SqlConnection();
        konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
        SqlCommand komanda = new SqlCommand();
        komanda.Connection = konekcija;
        komanda.CommandText = "SELECT p.den, p.doo, p.od, pr.Predmet, k2.Ime, k2.Prezime FROM Predava AS p, Slusa AS s,Korisnici AS k1, Korisnici as k2, Predmet AS pr WHERE p.prof_ID=k2.KorIme AND p.prof_ID=s.s_prof AND pr.p_ID=s.s_predmet AND s.s_predmet=p.predmet_ID AND k1.KorIme=s.s_student AND k1.KorIme='" + ime + "'";
        List<string> den = new List<string>();
        List<string> od = new List<string>();
        List<string> doo = new List<string>();
        List<string> imence = new List<string>();
        List<string> predmet = new List<string>();
        Niza lista = new Niza();
        try
        {
            konekcija.Open();
            SqlDataReader citac = komanda.ExecuteReader();

            while (citac.Read())
            {
                string element1 = citac["den"].ToString();
                string element2 = citac["od"].ToString();
                string element3 = citac["doo"].ToString();
                string element4 = citac["Ime"].ToString() + " " + citac["Prezime"].ToString();
                string element5 = citac["Predmet"].ToString();
                den.Add(element1);
                od.Add(element2);
                doo.Add(element3);
                imence.Add(element4);
                predmet.Add(element5);
            }
            citac.Close();
        }
        catch (Exception er) { }
        finally
        {
            lista.den = den;
            lista.doo = doo;
            lista.imence = imence;
            lista.predmet = predmet;
            lista.od = od;
            konekcija.Close();
        }
        return lista;
    }
}
